// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import axios from 'axios'
import router from './router'
import Vant from 'vant'
import {Toast} from 'vant'
//import $ from 'jquery'
import 'vant/lib/index.css'
import './assets/css/common.css'
import './assets/fonts/iconfont.css'
import qs from 'qs'

Vue.config.productionTip = false
Vue.use(Vant)
Vue.filter('dateFormat', function (val) {
  var res = new Date(val);
  return [res.getFullYear(), res.getMonth()+1 < 10 ? '0'+(res.getMonth()+1) : (res.getMonth()+1), res.getDate() < 10 ? '0'+res.getDate() : res.getDate()].join('-');
});
let axiosAllRequestCount = 0
let canAxiosEnd = () => {
  if (axiosAllRequestCount <= 0) {
    axiosAllRequestCount = 0
    Toast.clear()
  }
}
// 实例对象
let instance = axios.create({
  timeout: 3000,
  baseURL: process.env.NODE_ENV === 'production' ? '' : 'http://114.249.188.135:1080',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  }
})
// 请求拦截器
instance.interceptors.request.use(
  config => {
    axiosAllRequestCount++
    config.data = qs.stringify(config.data) // 转为formdata数据格式
    Toast.loading({
      message: '加载中...',
      forbidClick: true,
      loadingType: 'spinner',
    });
    return config
  }, function (error) {
    axiosAllRequestCount--
    canAxiosEnd()
    return Promise.reject(error)
  }
)
instance.interceptors.response.use(
 response => {
   axiosAllRequestCount--
    canAxiosEnd()
    let data = response.data
    return data
 }
)
Vue.prototype.$ajax = instance;
Vue.prototype.$searchParams = {};
//Vue.prototype.$ = $;
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
